package han.chensing.latos.maker.util;

import com.badlogic.gdx.audio.Music;

public class MusicStopWatch {

    private int updateLoopTime=20;
    private float nowTime;
    private int nowUpdateLeftTime=0;
    private long stopStartTime;
    private float nowTempTime;
    private boolean paused=true;

    public void updateNowTime(Music music){
        if(--nowUpdateLeftTime<=0){
            nowUpdateLeftTime=updateLoopTime;
            nowTime=music.getPosition();
            stopStartTime=System.currentTimeMillis();
            nowTempTime=nowTime;
            return;
        }
        if (!paused)
            nowTime=nowTempTime+(System.currentTimeMillis()-stopStartTime)/1000f;
    }

    public void forceUpdateNowTime(Music music){
        nowUpdateLeftTime=0;
        updateNowTime(music);
    }

    public void setPaused(boolean paused) {
        this.paused = paused;
    }

    public float getNowTime() {
        return nowTime;
    }

    public void setUpdateLoopTime(int updateLoopTime) {
        this.updateLoopTime = updateLoopTime;
    }
}
